// 教程 https://blog.csdn.net/naozheyun2998/article/details/138145414

let ary = [3, 8, 1, 9, 4, 5, 6, 2, 7];
const insertSort = (arr) => {
  const len = arr.length;
  for (let i = 1; i < len; i++) {
    let temp = arr[i];
    let j = i;

    while (j > 0 && arr[j - 1] > temp) {
      arr[j] = arr[j - 1];
      j--;
    }
    arr[j] = temp;
  }

  return arr;
};

console.log(insertSort(ary));
console.log(insertSort([6, 3, 1, 20, 5]));
